package top.dingwen.io.treasure.sensitive.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.houbb.sensitive.core.api.SensitiveUtil;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.dingwen.io.treasure.base.util.mapstruct.MapstructUtils;
import top.dingwen.io.treasure.enums.validator.annotation.EnumValues;
import top.dingwen.io.treasure.mybatisplus.util.MyPageHelper;
import top.dingwen.io.treasure.mybatisplus.util.QueryUtils;
import top.dingwen.io.treasure.mybatisplus.web.mybatisplus.AbstractBaseControllerMybatisPlusImpl;
import top.dingwen.io.treasure.redis.annotation.ReSubmit;
import top.dingwen.io.treasure.sensitive.constant.SensitiveConstant;
import top.dingwen.io.treasure.sensitive.constant.WordsType;
import top.dingwen.io.treasure.sensitive.core.WordsFilter;
import top.dingwen.io.treasure.sensitive.param.WordsParam;
import top.dingwen.io.treasure.sensitive.param.WordsQueryParam;
import top.dingwen.io.treasure.sensitive.result.SensitiveWordsResult;
import top.dingwen.io.treasure.sensitive.po.SensitiveWords;
import top.dingwen.io.treasure.sensitive.result.SensitiveUserResult;
import top.dingwen.io.treasure.sensitive.service.ISensitiveWordsService;
import top.dingwen.io.treasure.webplus.core.validator.valid.ValidGroup;
import top.dingwen.io.treasure.webplus.result.PageResult;
import top.dingwen.io.treasure.webplus.result.Result;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 脱敏场景场景默认API
 *
 * @author dingwen
 * @since 2023/5/23 11:07
 */
@RestController
@Slf4j
@RequestMapping("common/sensitives")
public class SensitiveIndexController extends AbstractBaseControllerMybatisPlusImpl<SensitiveWords,WordsParam,WordsQueryParam,SensitiveWordsResult> {

    @Resource
    private WordsFilter wordsFilter;

    @Resource
    private ISensitiveWordsService sensitiveWordsService;

    /**
     * 敏感词分页查询
     *
     * @param wordsContent 敏感词内容
     * @param wordsType 敏感词类型
     * @param gmtCreateStart 创建开始时间
     * @param gmtCreateEnd 创建结束时间
     * @return {@link PageResult}<{@link SensitiveWordsResult}> 敏感词分页列表
     */
    @GetMapping
    public PageResult<SensitiveWordsResult> wordPage(@RequestParam(required = false) String wordsContent,
                                                     @RequestParam(required = false) @EnumValues(enumClass = WordsType.class,required = false) WordsType wordsType,
                                                     @RequestParam(required = false) Date gmtCreateStart,
                                                     @RequestParam(required = false) Date gmtCreateEnd
                                                     ) {
        WordsQueryParam query = new WordsQueryParam(wordsContent,wordsType,gmtCreateStart,gmtCreateEnd);
        QueryWrapper<SensitiveWords> lq = QueryUtils.buildQueryWrapper(query);
        MyPageHelper.startPage(query);
        Page<SensitiveWords> sensitiveWordsPage = (Page<SensitiveWords>) sensitiveWordsService.list(lq);
        return page(MapstructUtils.convert(sensitiveWordsPage, SensitiveWordsResult.class), sensitiveWordsPage.getTotal());
    }

    /**
     * 修改敏感词
     *
     * @param param 敏感词入参
     * @return {@link Result}<{@link Boolean}>
     */
    @ReSubmit
    @Override
    @PutMapping
    public Result<Boolean> modify(@RequestBody @Validated({ValidGroup.Modify.class}) WordsParam param) {
        SensitiveWords sensitiveWords = MapstructUtils.convert(param, SensitiveWords.class);
        return genResult(sensitiveWordsService.updateById(sensitiveWords));
    }

    /**
     * 添加敏感词
     *
     * @param request 敏感词数据
     * @return {@link Result}
     */
    @PostMapping
    @ReSubmit
    @Override
    public Result<String> create(@RequestBody @Validated({ValidGroup.Save.class}) WordsParam request) {
        SensitiveWords sensitiveWords = MapstructUtils.convert(request, SensitiveWords.class);
        return genResult(sensitiveWordsService.save(sensitiveWords));
    }

    /**
     * 获取敏感词集
     *
     * @param text 提交的文本
     * @return {@link Result }<{@link String }> 结果
     */
    @GetMapping("/test/words")
    public Result<List<String>> getSensitiveWords(@RequestParam String text) {
        return success(wordsFilter.fetchWords(text));
    }

    /**
     * 获取敏感词处理结果
     *
     * @param text 提交的文本
     * @return {@link Result }<{@link String }> 结果
     */
    @GetMapping("/test/results")
    public Result<String> getHandlerResult(@RequestParam String text) {
        return success(wordsFilter.replace(text));
    }

    /**
     * 测试用户信息脱敏
     *
     * @return {@link Result }<{@link SensitiveUserResult }> 用户信息
     */
    @GetMapping("/test/users/infos")
    public Result<SensitiveUserResult> getUserInfo() {
        SensitiveUserResult userInfo = new SensitiveUserResult();
        userInfo.setUsername("张三");
        userInfo.setPassword("123456");
        userInfo.setPassport("CN1234567");
        userInfo.setPhone("13066668888");
        userInfo.setAddress("中国上海市浦东新区外滩18号");
        userInfo.setEmail("whatanice@code.com");
        userInfo.setBirthday("20220831");
        userInfo.setGps("66.888888");
        userInfo.setIp("127.0.0.1");
        userInfo.setMaskAll("可恶啊我会被全部掩盖");
        userInfo.setMaskHalf("还好我只会被掩盖一半");
        userInfo.setMaskRange("我比较灵活指定掩盖范围");
        userInfo.setBandCardId("666123456789066");
        userInfo.setIdNo("360123202306018888");
        log.info("{},测试日志脱敏，用户信息[{}]", SensitiveConstant.SEN_L_P_S,SensitiveUtil.desJson(userInfo));
        return success(SensitiveUtil.desCopy(userInfo));
    }


}
